<?php
/**
 * Created by PhpStorm.
 * User: liang
 * Email:1050187159@qq.com
 * Date: 2018/8/15
 * Time: 23:28
 */
namespace Admin\Controller;

class InvestsController extends BaseController
{
    private $invest = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->invest = D('Invest');
    }

    public function index()
    {
        //查询满足要求的总记录数
        $count = $this->invest->count();
        //实例化分页类 传入总记录数和每页显示的记录数(10)
        $Page = new \Think\Page($count,10);
        $Page->setConfig('next',C('PAGE_CONFIG.next'));
        $Page->setConfig('prev',C('PAGE_CONFIG.prev'));
        $Page->setConfig('last',C('PAGE_CONFIG.last'));
        $Page->setConfig('first',C('PAGE_CONFIG.first'));
        $Page->lastSuffix=C('PAGE_CONFIG.lastSuffix');//在此设置一行
        $show = $Page->show();// 分页显示输出
        //进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $invests = $this->invest->order('sort asc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign(array(
            // 赋值数据集
            'invests' => $invests,
            // 赋值分页输出
            'show' => $show
        ));
        $this->display('Invests/list');
    }

    public function add()
    {
        if (!IS_POST) {
            $this->display('Invests/add');
            exit;
        }
        if ($this->invest->investsAdd()) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->invest->getError());
        }
    }

    public function modify()
    {
        $invest_id = I('get.id');
        if (!IS_POST) {
            $invest = $this->invest->where(array('id' => array('eq', $invest_id)))->find();
            $invest['content'] = htmlspecialchars_decode($invest['content']);
            $this->assign(array('invest' => $invest));
            $this->display('Invests/edit');
            exit;
        }
        if($this->invest->investsModify()) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->invest->getError());
        }
    }

    public function destroy()
    {
        $id = I('get.id');

        $invest = $this->invest->find($id);

        unlink(APP_PATH.$invest['image']);
        unlink(APP_PATH.$invest['mobile_image']);

        $result = $this->invest->delete($id);
        if ($result !== false) {
            $this->ajaxReturn('true');
        } else {
            $this->ajaxReturn($this->invest->getError());
        }
    }
}